<script setup lang="ts">
import {useRouter} from "vue-router";
import {onMounted, ref, watch} from "vue";
import {Toast} from "vant";
import myAxios from "../../api/myAxios";

const userDefinedTag = ref("")
const addUserDefinedTag = () => {
  if (userDefinedTag.value !== "") {
    activeIds.value.push(userDefinedTag.value)
    userDefinedTag.value = ""
  } else {
    Toast("请先输入标签名")
  }
}

let router = useRouter();
const originTagList = [
  // 保持原有标签定义不变...
  {
    text: '性别',
    children: [
      {text: '男', id: '男'},
      {text: '女', id: '女'},
      {text: '保密', id: '保密'}
    ],
  },
  {
    text: '年级',
    children: [
      {text: '高一', id: '高一'},
      {text: '高二', id: '高二'},
      {text: '高三', id: '高三'},
      {text: '大一', id: '大一'},
      {text: '大二', id: '大二'},
      {text: '大三', id: '大三'},
      {text: '大四', id: '大四'},
      {text: '研究生', id: '研究生'},
      {text: '已工作', id: '已工作'},
    ],
  },
  {
    text: "编程",
    children: [
      {text: "Java", id: "Java"},
      {text: "Python", id: "Python"},
      {text: "C++", id: "C++"},
      {text: "JavaScript", id: "JavaScript"},
      {text: "Ruby", id: "Ruby"},
      {text: "PHP", id: "PHP"},
      {text: "Swift", id: "Swift"},
      {text: "Objective-C", id: "Objective-C"},
      {text: "C#", id: "C#"},
      {text: "Go", id: "Go"},
      {text: "Kotlin", id: "Kotlin"},
      {text: "TypeScript", id: "TypeScript"},
      {text: "Rust", id: "Rust"},
      {text: "Lua", id: "Lua"},
      {text: "Perl", id: "Perl"},
      {text: "Scala", id: "Scala"},
      {text: "Dart", id: "Dart"},
      {text: "R", id: "R"},
      {text: "MATLAB", id: "MATLAB"},
      {text: "Haskell", id: "Haskell"},
      {text: "Julia", id: "Julia"},
      {text: "Groovy", id: "Groovy"},
      {text: "Shell", id: "Shell"},
      {text: "Assembly", id: "Assembly"},
      {text: "F#", id: "F#"},
      {text: "Clojure", id: "Clojure"},
      {text: "Erlang", id: "Erlang"},
      {text: "Lisp", id: "Lisp"},
      {text: "Prolog", id: "Prolog"},
      {text: "Smalltalk", id: "Smalltalk"}
    ]
  },
  {
    text: '兴趣爱好',
    children: [
      {text: '阅读', id: '阅读'},
      {text: '旅游', id: '旅游'},
      {text: '游戏', id: '游戏'},
      {text: '音乐', id: '音乐'},
      {text: '电影', id: '电影'},
      {text: '美食', id: '美食'},
      {text: '健身', id: '健身'},
      {text: '编程', id: '编程'},
      {text: '摄影', id: '摄影'},
      {text: '绘画', id: '绘画'},
      {text: '写作', id: '写作'},
      {text: '手工', id: '手工'},
      {text: '园艺', id: '园艺'},
      {text: '钓鱼', id: '钓鱼'},
      {text: '篮球', id: '篮球'},
      {text: '足球', id: '足球'},
      {text: '棒球', id: '棒球'},
      {text: '乒乓球', id: '乒乓球'},
      {text: '羽毛球', id: '羽毛球'},
      {text: '网球', id: '网球'}
    ]
  },
  {
    text: '职业',
    children: [
      {text: '医生', id: '医生'},
      {text: '律师', id: '律师'},
      {text: '教师', id: '教师'},
      {text: '工程师', id: '工程师'},
      {text: '程序员', id: '程序员'},
      {text: '设计师', id: '设计师'},
      {text: '销售', id: '销售'},
      {text: '市场营销', id: '市场营销'},
      {text: '会计师', id: '会计师'},
      {text: '金融分析师', id: '金融分析师'},
      {text: '投资银行家', id: '投资银行家'},
      {text: '记者', id: '记者'},
      {text: '编辑', id: '编辑'},
      {text: '作家', id: '作家'},
      {text: '演员', id: '演员'},
      {text: '导演', id: '导演'},
      {text: '音乐家', id: '音乐家'},
      {text: '画家', id: '画家'},
      {text: '建筑师', id: '建筑师'},
      {text: '厨师', id: '厨师'},
      {text: '学生', id: '学生'}
    ]
  }
];

// 初始化标签列表
let tagList = ref(originTagList);
const searchText = ref('');

// 初始化时添加类型注解，明确 activeIds 是字符串数组
const activeIds = ref<string[]>([]);
// 初始化 mainActiveIndex 并添加类型注解
const activeIndex = ref<number>(0);

// 添加加载状态标识
const isLoading = ref(true);

onMounted(async () => {
  try {
    let res = await myAxios.get("/user/tags");
    if (res?.code === 0) {
      activeIds.value = res.data || [];
    } else {
      Toast(res?.message || "获取标签失败");
    }
  } catch (error) {
    console.error("获取标签出错:", error);
    Toast("网络错误，请稍后重试");
  } finally {
    isLoading.value = false;
  }
})

// 优化搜索功能，处理空结果情况
const onSearch = () => {
  if (!searchText.value.trim()) {
    // 搜索内容为空时恢复原始列表
    tagList.value = originTagList;
    return;
  }

  // 过滤标签列表
  tagList.value = originTagList
      .map(parentTag => {
        const filteredChildren = parentTag.children
            .filter(item => item.text.includes(searchText.value));

        return {
          ...parentTag,
          children: filteredChildren
        };
      })
      // 移除没有子标签的分类
      .filter(parentTag => parentTag.children.length > 0);
};

const close = (tag: string) => {
  activeIds.value = activeIds.value.filter((item) => item !== tag);
};

const updateTag = async () => {
  try {
    let res = await myAxios.put("/user/update/tags", activeIds.value);
    if (res?.code === 0) {
      await router.replace("/user");
      Toast.success("标签更新成功");
    } else {
      Toast(res?.message || "更新失败");
    }
  } catch (error) {
    console.error("更新标签出错:", error);
    Toast("网络错误，请稍后重试");
  }
}

// 监听 activeIds 变化，确保其有效性
watch(activeIds, (newIds) => {
  // 确保 activeIds 始终是数组
  if (!Array.isArray(newIds)) {
    activeIds.value = [];
  }
});
</script>

<template>
  <form action="/public">
    <van-search
        v-model="searchText"
        placeholder="请输入要搜索的标签"
        @search="onSearch"
    />

    <!-- 添加加载状态显示 -->
    <van-loading v-if="isLoading" type="spinner" size="30px" text="加载中..."/>

    <div v-else-if="activeIds.length > 0">
      <van-divider content-position="center">已选标签</van-divider>
      <van-row style="padding: 16px">
        <van-col v-for="tag in activeIds" :key="tag">
          <van-tag closeable size="large" type="primary" @close="close(tag)" style="margin: 5px;">
            {{ tag }}
          </van-tag>
        </van-col>
      </van-row>
    </div>

    <van-divider content-position="center">自定义标签</van-divider>
    <van-cell-group inset>
      <van-field v-model="userDefinedTag" placeholder="请添加标签">
        <template #button>
          <van-button round size="small" type="success" @click="addUserDefinedTag" icon="passed">添加</van-button>
        </template>
      </van-field>
    </van-cell-group>

    <van-divider content-position="center">标签列表</van-divider>

    <!-- 确保标签列表有数据时才显示组件 -->
    <van-tree-select
        v-if="!isLoading && tagList.length > 0"
        v-model:active-id="activeIds"
        v-model:main-active-index="activeIndex"
        :items="tagList"
        height="65vw"
    />

    <!-- 处理无搜索结果的情况 -->
    <div v-else-if="!isLoading && searchText.value.trim()">
      <van-empty image="search" description="没有找到相关标签"/>
    </div>
  </form>

  <div style="margin: 50px 100px">
    <van-button icon="passed" round block type="primary" @click="updateTag" :loading="isLoading">
      完成
    </van-button>
  </div>
</template>

<style scoped>
/* 可以添加一些加载状态的样式 */
.van-loading {
  padding: 20px 0;
  text-align: center;
}
</style>